﻿using System;
using System.IO;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;

namespace FileMerger
{
    public partial class MainForm : Form
    {
        private string file1Path = "";
        private string file2Path = "";

        public MainForm()
        {
            InitializeComponent();
        }

        private void btnSelectFile1_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                file1Path = openFileDialog.FileName;
                txtFile1.Text = file1Path;
            }
        }

        private void btnSelectFile2_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                file2Path = openFileDialog.FileName;
                txtFile2.Text = file2Path;
            }
        }

        private void btnMerge_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(file1Path) || string.IsNullOrEmpty(file2Path))
            {
                MessageBox.Show("请先选择两个文件！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            try
            {
                // 读取文件内容
                string content1 = File.ReadAllText(file1Path);
                string content2 = File.ReadAllText(file2Path);

                // 合并内容
                string mergedContent = content1 + Environment.NewLine + content2;

                // 创建目标目录
                string dataDir = Path.Combine(Application.StartupPath, "Data");
                Directory.CreateDirectory(dataDir);

                // 生成新文件名
                string newFileName = $"merged_{DateTime.Now:yyyyMMddHHmmss}.txt";
                string newFilePath = Path.Combine(dataDir, newFileName);

                // 写入新文件
                File.WriteAllText(newFilePath, mergedContent);

                MessageBox.Show($"文件合并成功！\n保存路径：{newFilePath}",
                              "成功",
                              MessageBoxButtons.OK,
                              MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"操作失败：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}